package com.spica.spicaschool.ui.diaryadd

import android.text.SpannableStringBuilder
import android.view.LayoutInflater
import android.view.ViewGroup
import com.effective.android.panel.PanelSwitchHelper
import com.effective.android.panel.interfaces.listener.OnKeyboardStateListener
import com.spica.spicaschool.base.BaseFragment
import com.spica.spicaschool.databinding.LayoutAddDiaryBinding
import dagger.hilt.android.AndroidEntryPoint
import io.noties.markwon.Markwon
import io.noties.markwon.editor.MarkwonEditor
import timber.log.Timber
import javax.inject.Inject


@AndroidEntryPoint
class DiaryAddFragment2 : BaseFragment<LayoutAddDiaryBinding>() {


    /**
     * 解析工具
     */
    @Inject
    lateinit var markwon: Markwon

    /**
     * 预渲染
     */
    private lateinit var markwonEditor: MarkwonEditor

    private lateinit var helper: PanelSwitchHelper


    private var isPreview = false
        set(value) {
            if (value) {
                viewBinding.edittext.renderMD()
            } else {
                viewBinding.edittext.text = SpannableStringBuilder(viewBinding.edittext.getMD())
            }
            field = value
        }


    private fun initView() {
        viewBinding.edittext.setStylesBar(viewBinding.stylesbar)
        viewBinding.btnPreview.setOnClickListener {
            isPreview = !isPreview
        }
    }







    override fun setupViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): LayoutAddDiaryBinding = LayoutAddDiaryBinding.inflate(layoutInflater, container, false)

    override fun init() {
        markwonEditor = MarkwonEditor.create(markwon)
        initView()
        helper = PanelSwitchHelper.Builder(this)
            .addKeyboardStateListener(object : OnKeyboardStateListener {
                override fun onKeyboardChange(visible: Boolean, height: Int) {
                    Timber.e("键盘可见性：$visible")
                }
            })
            .addEditTextFocusChangeListener {
                onFocusChange { _, hasFocus ->
                    run {
                        Timber.e("输入框焦点${hasFocus}")
                    }
                }
            }
            .addContentScrollMeasurer {
                getScrollDistance { 0 }
                getScrollViewId { viewBinding.edittext.id }
            }
            .logTrack(true)
            .build()

    }

}